package com.ddm.controller.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddm.entity.webEntity.Regulation;
import com.ddm.entity.webEntity.Technique;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.webService.TechniqueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("WebTechniqueController")
@RequestMapping("/Web/technique")
@Api(tags = "技术信息相关接口")
@Slf4j
public class TechniqueController {

    @Autowired
    private TechniqueService techniqueService;

    @GetMapping
    @ApiOperation("查询所有技术信息")
    public Result<List<Technique>> list() {
        log.info("查询所有技术信息");
        List<Technique> techniques = techniqueService.getAllTechniques();
        return Result.success(techniques);
    }

    @GetMapping("/findById")  // 统一改为查询参数方式
    @ApiOperation("根据id查询技术信息")
    public Result<Technique> getById(
            @ApiParam(value = "技术信息id", required = true)
            @RequestParam(required = false) Integer id  // 保持与课程接口一致的参数传递方式
    ) {
        log.info("根据id查询技术信息:{}", id);
        Technique technique = techniqueService.getTechniqueById(id);
        return Result.success(technique);
    }

    @PostMapping
    @ApiOperation("添加技术信息")
    public Result<Technique> add(
            @RequestBody Technique technique
    ) {
        techniqueService.add(technique);
        return Result.success(technique);
    }

    @PutMapping
    @ApiOperation("更新技术信息")
    public Result<Technique> update(@RequestBody Technique technique) {  // 修正返回类型，统一返回实体
        if (technique.getId() == null) {
            return Result.error("更新失败，id不能为空");
        }
        techniqueService.update(technique);
        return Result.success(technique);  // 细化错误信息
    }

    @DeleteMapping("/delete")  // 统一改为查询参数方式
    @ApiOperation("删除技术信息")
    public Result<Technique> delete(
            @ApiParam(value = "技术信息id", required = true)
            @RequestParam(required = false) Integer id  // 保持与课程接口一致的参数传递方式
    ) {
        Technique deletedTechnique = techniqueService.getTechniqueById(id);  // 先查询再删除
        techniqueService.delete(id);
        return Result.success(deletedTechnique);
    }

    @GetMapping("/pageAll")
    @ApiOperation("分页查询技术信息")
    public Result<PageResult> getTechniquesByPage(
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,

            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ApiParam(value = "关键词（模糊查询）", required = false)
            @RequestParam(required = false) String key,
            @ApiParam(value = "类型(精确匹配)", required = false)
            @RequestParam(required = false) String type
    ) {
        Page<Technique> page = new Page<>(pageNum, pageSize);
        IPage<Technique> techniquePage = techniqueService.getTechniquesByPage(page,key,type);

        return Result.success(new PageResult(
                techniquePage.getTotal(),
                techniquePage.getRecords()
        ));
}
//    @GetMapping("/page")
//    @ApiOperation("根据分类ID分页查询技术信息")
//    public Result<PageResult> getTechniqueByCategoryIds(
//            @ApiParam(value = "分类ID列表（多个ID用 &ids= 拼接）", required = true)
//            @RequestParam("ids") List<Integer> ids,
//            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
//            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
//            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
//            @RequestParam(required = false, defaultValue = "10") Integer pageSize
//    ) {
//        log.info("根据分类ID分页查询技术信息，分类ID：{}，页码：{}，每页大小：{}",
//                ids, pageNum, pageSize);
//
//        Page<Technique> page = new Page<>(pageNum, pageSize);
//        IPage<Technique> TechinquePage = techniqueService.getTechniqueByCategoryIds(ids, page);
//
//        PageResult pageResult = new PageResult(
//                TechinquePage.getTotal(),
//                TechinquePage.getRecords()
//        );return Result.success(pageResult);}

}